package com.pyk.controller;


import com.pyk.entity.Question;
import com.pyk.entity.QuestionType;
import com.pyk.entity.Result;
import com.pyk.service.QuestionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.w3c.dom.ls.LSException;

import java.util.List;

@RestController
@RequestMapping("/question")
@Slf4j

public class QuestionController {

    @Autowired
    private QuestionService questionService;

    @PostMapping("/add")
    public Result add(@RequestBody Question question) {
        questionService.add(question);
        return Result.success();
    }

    @PutMapping("/update")
    public Result update(@RequestBody Question question) {

        if(questionService.findById(question.getQuestionId())!=null)
        {   log.info("第一步");
            questionService.updateById(question);
           log.info("最后一步");
            return Result.success("修改题目成功");
        }
        return Result.error("修改题目失败，题目不存在");


    }

    @GetMapping("/find")
    public Result<Question> find(@RequestParam Long id) {
        Question question=questionService.findById(id);
        return Result.success(question);
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestParam Long id) {
        if(questionService.findById(id)!=null)
        {
            questionService.deleteById(id);
            return Result.success("删除题目成功");
        }
        return Result.error("删除题目失败，题目不存在");
    }

    @GetMapping("/list")
    public Result<List<Question>> list(@RequestBody Question question)//题型 题目内容 相关课程id
    {
        List<Question> list=questionService.findAll(question);
        return Result.success(list);
    }
}
